<template>
  <div>
    <el-dialog
      title="新增班级"
      :close-on-click-modal="false"
      @close="visible = false"
      width="600px"
      :visible.sync="visible"
    >
      <el-form
        :model="classForm"
        :rules="classRule"
        ref="classForm"
        label-width="80px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="辅导老师" prop="teacher">
              <nice-select
                v-model="classForm.teacher"
                placeholder="请选择辅导老师"
                clearable
                :options="teacherList"
              ></nice-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班级名称" prop="class">
              <el-input
                type="text"
                placeholder="请输入班级名称"
                v-model="classForm.class"
                maxlength="4"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取消</el-button>
        <el-button
          type="primary"
          @click="dataFormSubmit()"
          size="small"
          v-preventReClick
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addClass } from '@/api/user.js'
import { cascadeQuery } from '@/api/api.js'
export default {
  props: {
    courseId: '',
    periodId: ''
  },
  data () {
    return {
      visible: false,
      teacherList: [],
      classForm: {
        teacher: '',
        class: ''
      },
      classRule: {
        class: [{ required: true, message: '请输入班级名称', trigger: 'blur' }],
        teacher: [
          { required: true, message: '请选择辅导老师', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['classForm'].resetFields()
        this.$refs['classForm'].clearValidate()
        this.getAllAutor()
      })
    },
    async getAllAutor (row) {
      let res = await cascadeQuery({
        table_name: 'nice_user_base',
        condition: [
          {
            user_type: [
              {
                '=': '4'
              }
            ]
          }
        ]
      })
      if (res[0]) {
        return
      }
      let data = res[1]
      if (data && data.code === 0) {
        if (data.data && data.data.length) {
          this.teacherList = data.data.map(ele => {
            return { label: ele.real_name, value: ele.sys_user_id }
          })
        }
      } else {
        this.$message.error(res[1].msg)
      }
    },
    dataFormSubmit () {
      console.log('this.classForm.teacher', this.classForm.teacher)
      this.$refs['classForm'].validate(valid => {
        if (valid) {
          this.submit()
        }
      })
    },
    async submit () {
      let res = await addClass({
        'nice_classes.classes_name': this.classForm.class,
        'nice_classes.course_id': this.courseId,
        'nice_classes.period': this.periodId,
        'nice_classes.tutor_id': this.classForm.teacher
      })
      if (res[0]) {
        return
      }
      if (res[1] && res[1].code === 0) {
        this.$message.success('新增班级成功！')
        this.visible = false
        this.$emit('refreshDataList')
      } else {
        this.$message.error(res[1].msg)
      }
    }
  }
}
</script>
